package ch08_Swing高级组件;

import ch07_GUI编程.BaseFrame;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;

public class JFileChooserDemo {
	public static void main(String[] args) {
		BaseFrame f = new BaseFrame("JFileChooser Demo");
		JLabel tip = new  JLabel();
		JFileChooser fc = new JFileChooser("D:/MyJavaSource/images");
		fc.setFileFilter(new FileNameExtensionFilter("图片文件","bmp","jpg","png","gif"));

		fc.setFileHidingEnabled(false);
		fc.setMultiSelectionEnabled(true);
		fc.setDialogTitle("请选择图片（可多选）");

		f.setLayout(new BorderLayout());
		f.add(tip,BorderLayout.CENTER);
		f.setLocationRelativeTo(null);
		f.showMe();

		int click = fc.showOpenDialog(f);
		String result = "<html>";
		if(click ==JFileChooser.APPROVE_OPTION) {
			File[] files = fc.getSelectedFiles();
			result += "您选择了以下文件:<ol>";
			for (File file : files) {
				result = result +"<li>"+file.getName()+"</li>";
			}
			result +="</ol>";
		}else if (click == JFileChooser.CANCEL_OPTION) {
			result +="您取消了选择";
		}
		result +="</html>";
		tip.setText(result);
	}
}
